﻿using System;
using UnityEngine;
#if UNITY_EDITOR
using System.Reflection;
#endif

namespace IQIGame.Onigao.Framework
{
    /// <summary>
    /// 自动检测性能较差的Message函数并报错
    /// Update这种性能较差的Message函数建议用MonoDriver的事件取代
    /// </summary>
    public abstract class MonoBehaviourOptimized : MonoBehaviour
    {
        private static string[] s_PoorPerformingFuncs = new string[] { "Update", "FixedUpdate", "LateUpdate", "OnApplicationQuit", "OnApplicationPause" };
        protected virtual void Awake()
        {
#if UNITY_EDITOR
            //method those MonoDriver implemented
            CheckMonoMessages(s_PoorPerformingFuncs);
#endif
        }

        protected virtual bool ignoreMonoMsgCheck()
        {
            return false;
        }

#if UNITY_EDITOR
        private void CheckMonoMessages(string[] methodNames)
        {
            if (methodNames == null)
            {
                return;
            }
            Type type = GetType();
            if (ignoreMonoMsgCheck())
            {
                return;
            }
            foreach (var methodInfo in GetType().GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance))
            {
                if (Array.IndexOf(methodNames, methodInfo.Name) != -1)
                {
                    Debug.LogError($"Method {methodInfo.Name} is invalid in FrameModule");
                    break;
                }
            }
        }
#endif
    }
}
